package com.kelvin.paopao.module.main.date.datedetail;

import com.hyphenate.easeui.utils.SpUtil;
import com.kelvin.kelvinlib.utils.ToastUtil;
import com.kelvin.paopao.api.RetrofitService;
import com.kelvin.paopao.api.responseBean.CommonListResponse;
import com.kelvin.paopao.api.responseBean.DateListModel;
import com.kelvin.paopao.utils.DialogMaker;

import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.annotations.NonNull;
import io.reactivex.disposables.Disposable;
import io.reactivex.functions.Action;
import io.reactivex.functions.Consumer;

/**
 * Created by MCK on 2017/5/10.
 */

public class DateDetailPresenter implements IDateDetailPresenter {

    private DateDetailActivity mView;

    public DateDetailPresenter(DateDetailActivity view) {
        this.mView = view;
    }


    @Override
    public void getDateDetail(int type, int id) {

    }

    @Override
    public void signup(int id) {
        RetrofitService.signup(String.valueOf(id)).doOnSubscribe(new Consumer<Disposable>() {
            @Override
            public void accept(@NonNull Disposable disposable) throws Exception {
                DialogMaker.showProgressDialog(mView, "报名中，请稍后~~");
            }
        }).subscribeOn(AndroidSchedulers.mainThread()).observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Consumer<CommonListResponse>() {
                    @Override
                    public void accept(@NonNull CommonListResponse commonListResponse) throws Exception {
                        DialogMaker.diamiss();
                        if (commonListResponse.getReason().equals("成功")) {
                            ToastUtil.showToast(mView, "报名成功");
                        } else {
                            ToastUtil.showToast(mView, "报名失败");
                        }
                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(@NonNull Throwable throwable) throws Exception {
                        DialogMaker.diamiss();
                    }
                }, new Action() {
                    @Override
                    public void run() throws Exception {
                        DialogMaker.diamiss();
                    }
                });
    }

    @Override
    public void isAlreadyJoin(int id) {
        RetrofitService.getMyAskDate(String.valueOf(id)).subscribe(new Consumer<CommonListResponse<DateListModel>>() {
            @Override
            public void accept(@NonNull CommonListResponse<DateListModel>
                                       dateListModelCommonListResponse) throws Exception {
                if (dateListModelCommonListResponse.getResult() != null && dateListModelCommonListResponse.getResult().size() != 0) {
                    mView.setAlreadyCheck();
                }
            }
        });
    }

    @Override
    public void cancel(int id) {
        RetrofitService.dateCancel(String.valueOf(id), SpUtil.getLoginHuanxin_id(mView)).subscribe(new Consumer<CommonListResponse>() {
            @Override
            public void accept(@NonNull CommonListResponse commonListResponse) throws Exception {
                if (commonListResponse.getReason().equals("成功")) {
                    //取消成功
                    ToastUtil.showToast(mView, "取消报名成功");
                    mView.finish();
                }
            }
        });
    }

    @Override
    public void getData(boolean isRefresh) {

    }

    @Override
    public void getMoreData() {

    }
}
